<!-- 我的查询 -->

<template>
  <WebSearch>
    <template v-if="!isHistory" #btn>
      <BtnsDeal />
    </template>

    <ParamStatusMulti bind="alarmLevelId" :dict="GETTER.ALARM_LEVEL" hasRight isCheckbox join />

    <ParamDateRange :label="`${CORE.MAP.ALARM}日期`" bind="alarmTimeBegin, alarmTimeEnd" history format />

    <ParamInput :label="`${CORE.MAP.ASSET}名称`" bind="deviceName" />

    <ParamTree
      v-if="!noSubSystem"
      :label="`${CORE.MAP.ALARM}分组 / 名称`"
      bind="alarmTreeId"
      :dict="$root.alarm_group_treeDict"
      @change="changeGroup"
    />
    <ParamSelect v-else :label="`${CORE.MAP.ALARM}名称`" bind="alarmTypeCode" :dict="moduleDict" />
  </WebSearch>
</template>

<script>
// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import props from './props'

// ============================== 导出组件 ============================== //

export default {
  /**
   * 名称定义 (实际使用名称)
   */
  name: 'MySearch',

  /**
   * 混入列表 (配置选项合并)
   */
  mixins: [CORE.mixWeb()],

  /**
   * 组件导入 (Abc.vue + Abc/index.vue)
   */
  ...CORE.components(require.context('./', true, /vue$/)),

  /**
   * 属性注册 (抽取以便查阅)
   */
  props,

  /**
   * 计算属性 (慎用箭头函数)
   */
  computed: {
    /**
     * 分组字典
     */
    groupDict() {
      const { $root: { alarm_group_treeDict: groupDict } } = this
      return groupDict.map(([, id, name]) => [id, name])
    },

    /**
     * 模块字典
     */
    moduleDict() {
      const { $root: { alarm_group_treeDict: groupDict }, param: { subSystemId } } = this
      const matched = groupDict.find(([, id]) => id === subSystemId) || {}
      return (matched[0] || []).map(([, , name, code]) => [code, name])
    },
  },

  /**
   * 本地方法 (轻量重要在前)
   */
  methods: {
    /**
     * 切换分组
     */
    changeGroup(treeId) {
      const obj = this.$root.alarm_group_treeMap[treeId]
      if (!treeId) {
        this.param.subSystemCode = undefined
        this.param.alarmTypeCode = undefined
      } else if (obj.parentId) {
        const parent = this.$root.alarm_group_treeMap[obj.parentId]
        this.param.subSystemCode = parent.subSystemCode
        this.param.alarmTypeCode = obj.alarmTypeCode
      } else {
        this.param.subSystemCode = obj.subSystemCode
        this.param.alarmTypeCode = undefined
      }
    },
  },
}
</script>
